@extends('backend.layouts.base')
@section('title', '导航菜单管理')
@section('content')
    <link rel="stylesheet" href="/static/css/backend/detail.css">
    <div class="all">
        <div id="app" v-cloak>
            <div class="vue-crumbs">
                <a @click="goParent()">导航菜单管理</a> > 编辑导航菜单
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="15%">
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">基本信息</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="上级菜单" >
                            <el-cascader
                                    v-model="nav_pid_arr"
                                    :options="parent_list"
                                    :props="{
                                      checkStrictly: true,
                                      label: 'name',
                                      value: 'id',
                                      children: 'sub_menu'
                                    }"
                                    clearable
                            ></el-cascader>
                        </el-form-item>
                        <el-form-item label="菜单名称">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="打开方式">
                            <el-select v-model="form.target" clearable placeholder="页面打开方式">
                                <el-option label="默认方式" value=""></el-option>
                                <el-option label="新窗口打开" value="_blank"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="链接类型">
                            <el-radio v-model="form.href_type" :label="0">url</el-radio>
                            <el-radio v-model="form.href_type" :label="1">分类</el-radio>
                            <el-radio v-model="form.href_type" :label="2">文章</el-radio>
                            <el-radio v-model="form.href_type" :label="3">页面</el-radio>
                        </el-form-item>
                        <el-form-item label="链接">
                            <template v-if="form.href_type===0">
                                <el-input v-model="form.href"></el-input>
                            </template>
                            <template v-else>
                                <el-input v-model="form.href" disabled></el-input>
                                <el-button v-if="form.href_type!==0" type="primary" @click="showHerfDialog()">选择</el-button>
                            </template>
                        </el-form-item>
                        <el-form-item label="排序">
                            <el-input v-model="form.display_order" type="number" ></el-input>
                        </el-form-item>
                        {{--<el-form-item label="分类类型">
                            <el-select v-model="form.type" clearable placeholder="类型">
                                <el-option label="文章" :value="0"></el-option>
                                <el-option label="页面" :value="1"></el-option>
                            </el-select>
                        </el-form-item>--}}
                        <el-form-item label="状态">
                            <template>
                                <el-switch
                                        v-model="form.status"
                                        :active-value="1"
                                        :inactive-value="0"
                                >
                                </el-switch>
                            </template>
                        </el-form-item>
                        <el-form-item label="图标" prop="icon">
                            <div class="upload-box" @click="openUpload('icon')" v-if="!form.icon">
                                <i class="el-icon-plus" style="font-size:32px"></i>
                            </div>
                            <div @click="openUpload('icon')" class="upload-boxed" v-if="form.icon">
                                <img :src="form.icon" alt="" style="width:150px;height:150px;border-radius: 5px;cursor: pointer;">
                                <div class="upload-boxed-text">点击重新上传</div>
                            </div>
                        </el-form-item>
                    </div>
                </div>
            </el-form>
            <div class="vue-page">
                <div class="vue-center">
                    <el-button type="primary" @click="submitForm('form')">提交</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </div>
            <!-- 选择链接数据 -->
            <el-dialog :title="href_data_title" :visible.sync="href_data_show" width="60%">
                <div>
                    <el-input v-model="href_data_keyword" style="width:60%;" :placeholder="href_data_placeholder"></el-input>
                    <el-button @click="getHrefData" >搜索</el-button>
                </div>
                <el-table :data="href_data_list" style="width: 100%;height:500px;overflow:auto" v-loading="href_data_loading">
                    <el-table-column label="ID" prop="id" align="center" width="100px"></el-table-column>
                    <el-table-column v-if="form.href_type==1" label="分类名称" prop="name" align="center"></el-table-column>
                    <el-table-column v-else-if="form.href_type==2" label="文章标题" prop="title" align="center"></el-table-column>
                    <el-table-column v-else-if="form.href_type==3" label="页面标题" prop="title" align="center"></el-table-column>
                    <el-table-column prop="refund_time" label="操作" align="center" width="320">
                        <template slot-scope="scope">
                            <el-button @click="chooseHref(scope.row)">
                                选择
                            </el-button>

                        </template>
                    </el-table-column>
                </el-table>
                <span slot="footer" class="dialog-footer">
                    {{--<el-button @click="closeHotel">取 消</el-button>--}}
                </span>

            </el-dialog>
            <upload-img :upload-show="uploadShow" :name="chooseImgName" @replace="changeProp" @sure="sureImg"></upload-img>
        </div>

    </div>
    @include('backend.public.uploadImg')
    <script>
        var app = new Vue({
            el:"#app",
            delimiters: ['[[', ']]'],
            name: 'test',
            data() {
                return{
                    id:0,
                    nav_id:'',
                    nav_pid_arr:[],
                    form:{
                        nav_id:'',
                        pid:'',
                        name:'',
                        status:1,
                        display_order:0,
                        target:'',
                        href_type:0,
                        href_id:0,
                        href:'',
                        icon:''
                    },
                    parent_list:[],
                    rules:{
                        name:{ required: true, message: '请输入名称'}
                    },
                    submit_url:'',
                    uploadShow:false,
                    chooseImgName:'',
                    href_data_title:'',
                    href_data_placeholder:'',
                    href_data_keyword:'',
                    href_data_show:false,
                    href_data_loading:false,
                    href_data_list:[]
                }
            },
            created() {

            },
            mounted() {
                this.id = getParam("id");
                this.nav_id = getParam('nav_id');
                if(this.id) {
                    this.is_edit = true;
                    this.submit_url = `{!! absolute_admin_url('sys.navMenu.update') !!}`;
                    this.getData();
                } else {
                    this.form.pid = parseInt(getParam("pid"));
                    if(this.form.pid){
                        this.nav_pid_arr = [this.form.pid];
                    }
                    this.form.nav_id = getParam("nav_id");
                    this.submit_url = `{!! absolute_admin_url('sys.navMenu.create') !!}`;
                }
                this.getParent();
            },
            methods: {
                getHrefData() {
                    let params = {};
                    let url = '';
                    if(this.form.href_type==1){
                        //分类
                        url = '{!! absolute_admin_url('content.category.query') !!}';

                    }else if(this.form.href_type==2){
                        //文章
                        url = '{!! absolute_admin_url('content.article.query') !!}';
                    }else if(this.form.href_type==3){
                        //页面
                        url = '{!! absolute_admin_url('content.page.query') !!}';
                    }else{
                        return;
                    }
                    params['kwd'] = this.href_data_keyword;
                    this.$http.post(url,params).then(function(response) {
                        if (response.data.result) {
                            this.href_data_list = response.data.data;
                        } else {
                            this.$message({
                                message: response.data.msg,
                                type: 'error'
                            });
                        }
                    }, function(response) {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    });
                },
                getParent() {
                    let json = {
                        nav_id:this.nav_id
                    };
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('sys.navMenu.getList') !!}',{search:json}).then(function(response) {
                        if (response.data.result) {
                            this.parent_list = response.data.data;
                            loading.close();

                        } else {
                            this.$message({
                                message: response.data.msg,
                                type: 'error'
                            });
                        }
                        loading.close();

                    }, function(response) {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                        loading.close();
                    });
                },
                goParent() {
                    window.location.href = `{!! absolute_admin_url('sys.navMenu.index') !!}`+'&nav_id='+this.nav_id;
                },
                getData() {
                    let json = {};
                    if(this.id){
                        json['id'] = this.id;
                    }
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('sys.navMenu.getDetail') !!}',json).then(function (response) {
                            if (response.data.result){
                                this.form = {
                                    ...response.data.data,
                                };
                                if(this.form.pid){
                                    this.nav_pid_arr = [this.form.pid];//组件要数组格式
                                }
                            }
                            else {
                                this.$message({message: response.data.msg,type: 'error'});
                            }
                            loading.close();
                        },function (response) {
                            this.$message({message: response.data.msg,type: 'error'});
                            loading.close();
                        }
                    );
                },
                showHerfDialog(){
                    if(this.form.href_type==1){
                        //分类
                        this.href_data_title = '选择分类';
                        this.href_data_placeholder = '分类信息';
                    }else if(this.form.href_type==2){
                        //文章
                        this.href_data_title = '选择文章';
                        this.href_data_placeholder = '文章信息';
                    }else if(this.form.href_type==3){
                        //页面
                        this.href_data_title = '选择页面';
                        this.href_data_placeholder = '页面信息';
                    }else{
                        return;
                    }
                    this.href_data_keyword = '';
                    this.href_data_list = [];
                    this.href_data_show = true;
                },
                chooseHref(item){
                    this.form.href_id=item.id;
                    if(this.form.href_type==1){
                        //分类;
                        this.form.href='{!! absolute_url('/article/list') !!}'+'?cid='+item.id;
                    }else if(this.form.href_type==2){
                        //文章
                        this.form.href='{!! absolute_url('/article/detail') !!}'+'?id='+item.id;
                    }else if(this.form.href_type==3){
                        //页面
                        this.form.href='{!! absolute_url('/page') !!}'+'?id='+item.id;
                    }else{
                        return;
                    }
                    this.href_data_show = false;
                },
                submitForm(formName) {
                    let that = this;
                    let json = {
                        pid:this.form.pid,
                        nav_id:this.form.nav_id,
                        status:this.form.status,
                        target:this.form.target,
                        href_type:this.form.href_type,
                        href_id:this.form.href_id,
                        href:this.form.href,
                        display_order:this.form.display_order,
                        name:this.form.name,
                        icon:this.form.icon
                    };
                    if(this.nav_pid_arr && this.nav_pid_arr.length>0){
                        json.pid = this.nav_pid_arr[this.nav_pid_arr.length-1];
                    }
                    if(this.id) {
                        json.id = this.id
                    }
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                            this.$http.post(this.submit_url,{params:json}).then(response => {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.goParent();
                                } else {
                                    this.$message({message: response.data.msg,type: 'error'});
                                }
                                loading.close();
                            },response => {
                                loading.close();
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },

                goBack() {
                    history.go(-1)
                },
                openUpload(str) {
                    this.chooseImgName = str;
                    this.uploadShow = true;
                },
                changeProp(val) {
                    if(val == true) {
                        this.uploadShow = false;
                    }
                    else {
                        this.uploadShow = true;
                    }
                },
                sureImg(name,image,image_url) {
                    this.form[name] = image_url;
                },
                clearImg(str) {
                    this.form[str] = "";
                    this.$forceUpdate();
                },
            },
        })

    </script>
@endsection
